﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Assembler_Compiler
{
    // represents a label with its name? address and size in bytes
    class Lable
    {
        public String name;
        public Int16 address;
        public Int16 size;

        public Lable()
        {
            this.name = string.Empty;
            this.address = 0;
            this.size = 0;
        }

        public Lable(String name, Int16 address, Int16 size)
        {
            this.name = name;
            this.address = address;
            this.size = size;
        }
    }

    // the Symbol Table (SYMTAB)
    // SYMTAB is used to store values (addresses) assigned to labels.
    static class SYMTAB
    {
        static List<Lable> labels;

        public static void Initialize()
        {
            labels = new List<Lable>();
        }

        public static void AddLable(String name, Int16 address, Int16 size = 0)
        {
            if (labels.Find(c => c.name == name) == null)
            {
                if (name[name.Length - 1] == ':')
                {
                    name = name.Remove(name.Length - 1);
                }
                
                labels.Add(new Lable(name, address, size));
            }
            else
            {
                throw new Exception("Duplicating labels: " + name);
            }
        }

        public static Int16 GetAddress(String name)
        {
            return labels.Find(c => c.name == name).address;
        }

        public static bool Contain(String name)
        {
            return labels.Find(c => c.name == name) != null;
        }
    }
}
